<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div id="test">
    FirstName: <input type="text" v-model="firstName"><br>
    LastName: <input type="text" v-model="lastName"><br>
    
    <p>fullName3(计算属性): {{fullName3}}</p>
    <br>
    <p>fullName4(监视): {{fullName4}}</p>
  </div>

  <script src="../js/vue.js"></script>
  <script>
    const vm = new Vue({
      el: "#test",
      data: {
        firstName: 'A',
        lastName: 'B',
        fullName4: 'A-B',
        a: {
          b: 'abc'
        }
      },

      computed: {
        fullName3() {
          // return this.firstName + '-' + this.lastName

          // 不异步返回计算结果  => 界面显示是空白的
          setTimeout(() => {
            return this.firstName + '-' + this.lastName
          }, 1000);
        }
      },

      watch: {
        // 被监视属性对应的表达式
        'a.b': function (value) {

        },

        firstName: {
          handler: function (newValue, oldValue) {
            console.log('watch firstName', newValue, oldValue)
            // 异步更新数据
            setTimeout(() => {
              this.fullName4 = newValue + '-' + this.lastName
            }, 1000);
          }
        }
      }
    })

    // 使用vm的$watch方法来监视
    vm.$watch('lastName', {
      handler: function (newValue, oldValue) {
        console.log('$watch lastName', newValue, oldValue)
        // 更新fullName4
        this.fullName4 = this.firstName + '-' + newValue
      }
    })
  </script>
</body>
</html>